/**
 *
 * ......................我佛慈悲.......................
 *                       _oo0oo_
 *                      o8888888o
 *                      88" . "88
 *                      (| -_- |)
 *                      0\  =  /0
 *                    ___/`---'\___
 *                  .' \\|     |// '.
 *                 / \\|||  :  |||// \
 *                / _||||| -卍-|||||- \
 *               |   | \\\  -  /// |   |
 *               | \_|  ''\---/''  |_/ |
 *               \  .-\__  '-'  ___/-. /
 *             ___'. .'  /--.--\  `. .'___
 *          ."" '<  `.___\_<|>_/___.' >' "".
 *         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *         \  \ `_.   \_ __\ /__ _/   .-` /  /
 *     =====`-.____`.___ \_____/___.-`___.-'=====
 *                       `=---='
 *
 *..................佛祖开光 ,永无BUG....................
 *         CSDN博客:http://blog.csdn.net/song5347
 *       github地址:https://github.com/lingsuiderongyan
 *             邮箱:lingsuiderongyan@163.com
 */
//自定义UItextView
import UIKit

//@IBDesignable 实时看到修改后属性的值
@IBDesignable
class LSComposeTextView: UITextView {
    
    //MARK: - 站位label
    private lazy var placeHolderLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(16)
        label.text = "被爱的人总是脾气暴躁,因为他们深信你不会离开他,主动爱的那个人脾气很好,因为他们害怕失去"
        label.textColor = UIColor.lightGrayColor()
        label.numberOfLines = 0
        
        return label
    }()
    
    //MARK: - 提供外界设置占位文字的属性
    //@IBInspectable 给xib/sb添加可以设置的属性
    @IBInspectable var placeHolder: String? {
        didSet {
            placeHolderLabel.text = placeHolder
        }
    }
    
    //MARK: - 重写font
    override var font: UIFont? {
        didSet {
            if font != nil {
                //当前外界设置的Font让站位label与其同步
                placeHolderLabel.font = font
            }
        }
    }
    
    //MARK: - 重写text
    override var text: String? {
        didSet {
            //根据文本内容显示站位label
            placeHolderLabel.hidden = hasText()
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    //MARK: - 使用xib加载
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    //MARK: - 添加控件设置约束
    private func setupUI() {
        //监听文字改变的通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textChange", name: UITextViewTextDidChangeNotification, object: nil)
        
        addSubview(placeHolderLabel)
        
        //使用系统约束
        placeHolderLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: placeHolderLabel, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1, constant: -10))
    }
    
    //MARK: - 监听文字改变的通知
    @objc private func textChange() {
//        if hasText() {
//            placeHolderLabel.hidden = true
//        } else {
//            placeHolderLabel.hidden = false
//        }
        placeHolderLabel.hidden = hasText()
    }
    
    //MARK: - 给子控件设置约束
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置坐标
        placeHolderLabel.frame.origin.x = 5
        placeHolderLabel.frame.origin.y = 7
    }
    
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}













